home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / dns / tsig.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  4.0 KB  |  113 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''DNS TSIG support.'''
  5. import hmac
  6. import struct
  7. import dns.exception as dns
  8. import dns.rdataclass as dns
  9. import dns.name as dns
  10.  
  11. class BadTime(dns.exception.DNSException):
  12.     """Raised if the current time is not within the TSIG's validity time."""
  13.     pass
  14.  
  15.  
  16. class BadSignature(dns.exception.DNSException):
  17.     '''Raised if the TSIG signature fails to verify.'''
  18.     pass
  19.  
  20. _alg_name = dns.name.from_text('HMAC-MD5.SIG-ALG.REG.INT.').to_digestable()
  21.  
  22. def hmac_md5(wire, keyname, secret, time, fudge, original_id, error, other_data, request_mac, ctx = None, multi = False, first = True):
  23.     '''Return a (tsig_rdata, mac, ctx) tuple containing the HMAC-MD5 TSIG rdata
  24.     for the input parameters, the HMAC-MD5 MAC calculated by applying the
  25.     TSIG signature algorithm, and the TSIG digest context.
  26.     @rtype: (string, string, hmac.HMAC object)
  27.     @raises ValueError: I{other_data} is too long
  28.     '''
  29.     if first:
  30.         ctx = hmac.new(secret)
  31.         ml = len(request_mac)
  32.         if ml > 0:
  33.             ctx.update(struct.pack('!H', ml))
  34.             ctx.update(request_mac)
  35.         
  36.     
  37.     id = struct.pack('!H', original_id)
  38.     ctx.update(id)
  39.     ctx.update(wire[2:])
  40.     if first:
  41.         ctx.update(keyname.to_digestable())
  42.         ctx.update(struct.pack('!H', dns.rdataclass.ANY))
  43.         ctx.update(struct.pack('!I', 0))
  44.     
  45.     long_time = time + 0x0L
  46.     upper_time = long_time >> 32 & 0xFFFFL
  47.     lower_time = long_time & 0xFFFFFFFFL
  48.     time_mac = struct.pack('!HIH', upper_time, lower_time, fudge)
  49.     pre_mac = _alg_name + time_mac
  50.     ol = len(other_data)
  51.     if ol > 65535:
  52.         raise ValueError, 'TSIG Other Data is > 65535 bytes'
  53.     
  54.     post_mac = struct.pack('!HH', error, ol) + other_data
  55.     if first:
  56.         ctx.update(pre_mac)
  57.         ctx.update(post_mac)
  58.     else:
  59.         ctx.update(time_mac)
  60.     mac = ctx.digest()
  61.     mpack = struct.pack('!H', len(mac))
  62.     tsig_rdata = pre_mac + mpack + mac + id + post_mac
  63.     if multi:
  64.         ctx = hmac.new(secret)
  65.         ml = len(mac)
  66.         ctx.update(struct.pack('!H', ml))
  67.         ctx.update(mac)
  68.     else:
  69.         ctx = None
  70.     return (tsig_rdata, mac, ctx)
  71.  
  72.  
  73. def validate(wire, keyname, secret, now, request_mac, tsig_start, tsig_rdata, tsig_rdlen, ctx = None, multi = False, first = True):
  74.     '''Validate the specified TSIG rdata against the other input parameters.
  75.  
  76.     @raises FormError: The TSIG is badly formed.
  77.     @raises BadTime: There is too much time skew between the client and the
  78.     server.
  79.     @raises BadSignature: The TSIG signature did not validate
  80.     @rtype: hmac.HMAC object'''
  81.     (adcount,) = struct.unpack('!H', wire[10:12])
  82.     if adcount == 0:
  83.         raise dns.exception.FormError
  84.     
  85.     adcount -= 1
  86.     new_wire = wire[0:10] + struct.pack('!H', adcount) + wire[12:tsig_start]
  87.     current = tsig_rdata
  88.     (aname, used) = dns.name.from_wire(wire, current)
  89.     current = current + used
  90.     (upper_time, lower_time, fudge, mac_size) = struct.unpack('!HIHH', wire[current:current + 10])
  91.     time = (upper_time + 0x0L << 32) + lower_time + 0x0L
  92.     current += 10
  93.     mac = wire[current:current + mac_size]
  94.     current += mac_size
  95.     (original_id, error, other_size) = struct.unpack('!HHH', wire[current:current + 6])
  96.     current += 6
  97.     other_data = wire[current:current + other_size]
  98.     current += other_size
  99.     if current != tsig_rdata + tsig_rdlen:
  100.         raise dns.exception.FormError
  101.     
  102.     time_low = time - fudge
  103.     time_high = time + fudge
  104.     if now < time_low or now > time_high:
  105.         raise BadTime
  106.     
  107.     (junk, our_mac, ctx) = hmac_md5(new_wire, keyname, secret, time, fudge, original_id, error, other_data, request_mac, ctx, multi, first)
  108.     if our_mac != mac:
  109.         raise BadSignature
  110.     
  111.     return ctx
  112.  
  113.